package com.one.shopping.ui.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.one.shopping.R;
import com.one.shopping.api.OneApi;
import com.one.shopping.entity.Result;
import com.one.shopping.entity.Users;
import com.one.shopping.ui.baseActivity.BaseActivity;
import com.one.shopping.util.CommonUtils;
import com.one.shopping.util.GsonUtils;

import org.apache.http.Header;

/**
 * @author 柯尊诚
 * @version 1.0
 * @date 创建时间：2016/1/12 10:26
 */
public class AddUserActivity extends BaseActivity {

    private EditText userNameField;
    private EditText userPwdField;
    private TextView userIdValues;
    private Button submitBtn;
    private boolean isUpdate = false;

    @Override
    protected int getContentViewId() {
        return R.layout.add_user_activity;
    }

    @Override
    protected void initView() {
        super.initView();
        userNameField = findView(R.id.add_user_name_field_tv);
        userPwdField = findView(R.id.add_user_passwd_field_tv);
        userIdValues = findView(R.id.add_user_id_values_tv);
        submitBtn = findView(R.id.add_user_submit_btn);
    }

    @Override
    protected void initData() {
        super.initData();

        Intent intent = getIntent();
        Users users = (Users)intent.getSerializableExtra("users");
        if (CommonUtils.isNullOrEmpty(users)) {
            setTitle("添加收银员");
            findView(R.id.add_user_id_layout).setVisibility(View.GONE);
        } else {
            setTitle("修改收银员");
            isUpdate = true;
            userNameField.setText(users.getName());
            userPwdField.setText(users.getPasswd());
            userIdValues.setText("" + users.getId());

        }
    }

    @Override
    protected void initListener() {
        super.initListener();

        submitBtn.setOnClickListener(onClickSubmitListener);
    }

    /**
     * 点击提交按钮
     */
    private View.OnClickListener onClickSubmitListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onNextSubmit();
        }
    };

    /**
     * 处理点击提交
     */
    private void onNextSubmit() {
        String name = userNameField.getText().toString().trim();
        String pwd = userPwdField.getText().toString().trim();
        if (CommonUtils.isNullOrEmpty(name)) {
            userNameField.setError("用户名不能为空");
            return;
        }
        if (CommonUtils.isNullOrEmpty(pwd)) {
            userPwdField.setError("密码不能为空");
            return;
        }

        Users users = new Users();
        users.setName(name);
        users.setPasswd(pwd);
        if (isUpdate) {
            int id = Integer.parseInt(userIdValues.getText().toString().trim());
            users.setId(id);
            OneApi.updateUser(users, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int i, Header[] headers, byte[] bytes) {
                    Result result = GsonUtils.fromJson(new String(bytes), Result.class);
                    makeToast(result.getMessage());
                    finish();
                }

                @Override
                public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                    makeToast("网络错误！");
                }
            });
        } else {
            OneApi.registerUser(users, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int i, Header[] headers, byte[] bytes) {
                    Result result = GsonUtils.fromJson(new String(bytes), Result.class);
                    if (result.getCode() == 500) {
                        makeToast(result.getMessage());
                    } else {
                        makeToast(result.getMessage());
                        finish();
                    }
                }

                @Override
                public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                    makeToast("网络错误！");
                }
            });
        }
    }
}
